/*
 * protocol.c
 *
 *  Created on: 29 Apr 2015
 *      Author: yiqi.pan
 */
#include "common.h"
#include "config.h"
#include "uart.h"
#include "protocol.h"

#define SOURCE_MASK         0b11000000
#define FROM_HOST_MASK      0b10000000
#define TOHOST_MASK     	0b01000000
#define SEQUENCE_MASK		0b00111111
#define packet_from_host(b) (((b)&0xC0) == 0x80)

volatile bool has_new_packet = false;

uint8_t receive_packet(uint8_t* data, uint8_t datalen)
{
	if(datalen==0)
		return 0;

	if(!packet_from_host(data[0]))
		return 1;

	/* not receive the whole header, continue receiving */
	if(datalen<PACKET_HEADER_LEN)
		return 0;


	if(datalen<PACKET_HEADER_LEN + data[PACKET_HEADER_LEN - 1])
	{
		/* not receive the whole packet, continue receiving */
		return 0;
	}
	else
	{
		/* now we got the whole packet, stop receiving until the packet is processed */
		uart_disable_rx(BT_UART);
		has_new_packet = true;
		return 0;
	}
}

uint8_t parse_packet(uint8_t* data, uint8_t datalen)
{
	uint8_t packetlen;
	packetlen = data[PACKET_HEADER_LEN - 1];
	
}
